// pages/invoice/list/index.js
var call = require("../../../utils/request.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isInit: false,  // 解决页面wx:if页面闪烁
    select_all: false,
    arr: [],
    arrLength: 0,
    amount: 0,
    listData: [],
    offset: 0,// 页码
    limit: 15, // 每页条数
    total: 0, // 数据总条数
    status: false, // 是否还有数据
    current: [
      {value: 'current', name: '本页全选',checked: false}
    ],
    all: [
      {value: 'all', name: '全部全选',checked: false},
    ],
    value: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getListData(this.data.offset,this.data.limit)
  },
  // 获取数据
  getListData (offset,limit) {
    call.getData('/api/Invoice/order',{ offset,limit },
    (data) => {
      if(data.code == 1){
        let rows = data.data.rows;
        if(rows.length > 0){
          let listData = this.data.listData;
          for(let item of data.data.rows){
            listData.push(item)
          }
          this.setData({ listData,total: data.data.total })
        }
      }else{
        wx.showToast({ icon: 'none',title: data.msg })
      }
      this.setData({isInit:true});
    },() => {
      doFail();
    })
  },
  onLoadList () {
    call.getData('/api/Invoice/order',{ offset: this.data.offset,limit: this.data.limit },
    (data) => {
      if(data.code == 1){
        let rows = data.data.rows;
        let listData = [];
        if(rows.length > 0){
          for(let item of data.data.rows){
            listData.push(item)
          }
        }
        this.data.all[0].checked = false;
        this.data.current[0].checked = false;
        this.setData({
          listData,
          total: data.data.total,
          amount: 0,
          arr: [],
          arrLength: 0, 
          all: this.data.all,
          current: this.data.current,
        })
      }else{
        wx.showToast({ icon: 'none',title: data.msg })
      }
      this.setData({isInit:true});
    },() => {
      doFail();
    })
  },
  // 改变
  checkboxChange (e) {
    var that = this;
    let index = e.currentTarget.dataset.index;
    if(e.detail.value.length > 0){
      that.data.listData[index].checked = true
    }else{
      that.data.listData[index].checked = false;
      this.data.current[0].checked = false;
      this.data.all[0].checked = false;
      this.setData({ all: this.data.all,current: this.data.current })
    }
    that.setData({
      listData: that.data.listData,
    })
    that.itineraryPrice()
    console.log(that.data.listData)
  },
  // 全选与反全选
  current(e) {
    console.log(e.detail.value)
    this.setData({
      value: e.detail.value,
    })
    let value = e.detail.value;
    if(value.length > 0){
      this.data.all[0].checked = false;
      for (let item of this.data.listData) {
        item.checked = true;
      }
      this.setData({
        all: this.data.all,
        listData: this.data.listData,
      })
      this.itineraryPrice();
    }else{
      for (let item of this.data.listData) {
        item.checked = false;
      }
      this.setData({
        listData: this.data.listData,
        amount: 0,
        arr: [],
        arrLength: 0,
      })
    }
  },
  All (e) {
    let value = e.detail.value;
    this.setData({
      value,
    })
    if(value.length > 0){
      this.data.current[0].checked = false;
      for (let item of this.data.listData) {
        item.checked = true;
      }
      call.getData('/api/Invoice/allcount',{},
      (data) => {
        if(data.code == 1){
          console.log(data)
          this.setData({
            amount: data.data.amount,
            arrLength: data.data.orderNum,
          })
        }else{
          wx.showToast({ icon: 'none',title: data.msg })
        }
        this.setData({isInit:true});
      },() => {
        doFail();
      })
      this.setData({
        current: this.data.current,
        listData: this.data.listData,
      })
    }else{
      for (let item of this.data.listData) {
        item.checked = false;
      }
      this.setData({
        listData: this.data.listData,
        amount: 0,
        arr: [],
        arrLength: 0,
      })
    }
  },
  itineraryPrice () {
    var that = this;
    let arr = [];
    let amount = 0;
    for (let i = 0; i < that.data.listData.length; i++) {
      if(that.data.listData[i].checked == true){
        arr.push(that.data.listData[i].id);
        amount += Number(that.data.listData[i].order_values);
      }
    }
    amount = Math.floor(amount * 100) / 100;
    that.setData({
      arr,
      arrLength: arr.length,
      amount
    })
  },
  // 下一步
  goNext () {
    var that = this;
    if(that.data.arr.length > 0 || that.data.value == 'all'){
      console.log(that.data.arr)
      let orders = JSON.stringify(that.data.arr);
      wx.navigateTo({
        url: '/pages/invoice/fillIn/index?orders=' + orders + "&amount=" + that.data.amount
      })
    }else{
      wx.showToast({
        icon: 'none',
        title: '未选择订单',
      })
    }
  },
  loadData () {
    
  },
  /**
   * 页面上拉触底事件的处理函数，与点击加载更多做同样的操作
   */
  onReachBottom () {
    let total = this.data.total;  // 总条数
    let offset = this.data.offset; // 偏移量
    let limit = this.data.limit; // 当前页显示条数
    this.setData({ offset: offset + 1 })
    offset = (offset + 1 ) * limit;
    console.log(offset)
    if(total > offset){
      console.log(1)
    }else{
      this.setData({ status: true })
    }
   
    if(this.data.status){
      console.log('没有数据了')
    }else{
      wx.showLoading({
        title: '玩命加载中...',
      });
      this.getListData(offset, limit);
      wx.hideLoading();
    }
  },
})